using System.ComponentModel;
using System.Security.AccessControl;
using System.Xml.Serialization;
using uTILLIty.Rave.Contract.StorageAdapter;

namespace uTILLIty.Rave.Contract.Configuration
{
  /// <summary>
  /// Represents an ACL entry
  /// </summary>
  /// <seealso cref="IStorageAdapter.ValidateSecurityEntry"/>
	public class SecurityEntry
  {
    /// <summary>
    /// Initializes the instance
    /// </summary>
    public SecurityEntry()
    {
			AccessControlType = AccessControlType.Allow;
      InheritanceFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
      PropagationFlags = PropagationFlags.None;
	    MemberType = MemberType.Unknown;
    }

		/// <summary>
		/// Initializes the instance with the specified <paramref name="memberType"/>
		/// </summary>
		public SecurityEntry(MemberType memberType)
			: this()
		{
			MemberType = memberType;
		}

    /// <summary>
    /// Represents a reference to the <see cref="Member.ID"/> of the <see cref="Configuration.Members"/>
    /// collection within the <see cref="Configuration"/>
    /// </summary>
    public string MemberID { get; set; }

		/// <summary>
		/// Specified the type of member to search for
		/// </summary>
		[DefaultValue(MemberType.Unknown), XmlAttribute("type")]
	  public MemberType MemberType { get; set; }

    /// <summary>
    /// A comma-separated list of rights; to be interpreted by the implementing <see cref="IStorageAdapter"/>
    /// </summary>
    public string Rights { get; set; }

		/// <summary>
		/// The type of <see cref="Rights">rights</see>
		/// </summary>
		[DefaultValue(AccessControlType.Allow), XmlAttribute("access")]
		public AccessControlType AccessControlType { get; set; }

    /// <summary>
    /// If set to True, the group will only be created and memberships added, but not actually added to the ACLs
    /// of the currently processed folder
    /// </summary>
    [DefaultValue(false), XmlAttribute("dontAddToAcl")]
    public bool DontAddToAcl { get; set; }

    /// <summary>
    /// Inheritance flags specify the semantics of inheritance for access control entries (ACEs).
    /// </summary>
    [DefaultValue(InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit), XmlAttribute("inheritanceFlags")]
    public InheritanceFlags InheritanceFlags { get; set; }

    /// <summary>
    /// Progagation flags specify how the ACL is handled by child-elements
    /// </summary>
    [DefaultValue(PropagationFlags.None), XmlAttribute("propagationFlags")]
    public PropagationFlags PropagationFlags { get; set; }

		/// <summary>
		/// Returns a summary of settings for this instance
		/// </summary>
	  public override string ToString()
	  {
		  return string.Format("Member:{0} {3}{4} Inherit:{1} Propagate:{2}", 
				MemberID, InheritanceFlags, PropagationFlags, AccessControlType == AccessControlType.Allow ? "Allow:" : "Deny:", Rights.Replace(" ", ""));
	  }
  }
}